<?php
/**
 * @file ReadMoreController.php
 * @author LinLin
 * @date 2016年9月13日 下午2:42:07
 * @brief 
 */
namespace haibao\cms\business\control\pc\common;

use haibao\cms\business\control\pc\entity\ArticleEntity;
use haibao\cms\business\control\pc\action\ArticleAction;
use haibao\cms\business\control\ArticleController;
use haibao\frame\Environment;
use \haibao\cms\web\common\ftp;


class ReadMoreController extends \haibao\cms\business\control\pc\channel\ChannelController{

    public function __construct($updateTime = null,$controlId = '',$isPublish = ''){
        parent::__construct($updateTime,$controlId,$isPublish);
        $this->articleAction = ArticleAction::initArticleAction();
    }
    
    public function displayTemplateHtml(){
        $controlData = $this->getParameter(self::$CONTROL_DATA);
        $content = $this->getParameter(self::$CONTROL_CONTENT);
        $path = $this->generateHtmlPath(__CLASS__);
        $channelTag = self::getTopTags();
        //taglist readmore
        $dataCondition['dateType'] = 2;
        $dataCondition['limit'] = 10;
        $readmore = array();
        foreach($channelTag as $topTagId=>$val){
            $dataCondition['topTagId'] = $topTagId;
            $readmore[$topTagId] = $this->getMostReadData($dataCondition);
        }

        //$file= 'D:\code\php\www\trunk\sites\html\diytaghtml\taglistreadmore.php';
        //$file = '/data/web/www/trunk/sites/html/diytaghtml/taglistreadmore.php';
        $text = '<?php '."\n".'$readmore = '.var_export($readmore,true).';';
        $this->assign('text', $text);
        $isNewHtml = $this->generateHtml($path,$content);
        $content = $this->getHtml($path);
        $content = $this->replaceCDNLink($content);
        return $content;
    }
    
    //readmore
    public function getMostReadData($dataCondition){
        $articleEntity = new ArticleEntity();
        $articleEntity->setLevel(3);
        $articleEntity->setBigLevel(7);
        $articleEntity->setLimit($dataCondition['limit']);
        $articleEntity->setIsDate(true,2);
        $articleEntity->setIsProImage(true);
        $articleEntity->setIsLeftJoinTag(true);
        $articleEntity->setIsPrimaryTag(true);
        $articleEntity->setIsReturnTag(true);
        $articleEntity->setIsImageColl(true);
        if($dataCondition['topTagId'] != 6){
            $articleEntity->setIsParentTag(true);
            $articleEntity->setTagIds($dataCondition['topTagId']);
            $articleEntity->setIsConditionTag(true);
        }
        $articleEntity->setOrderByNew(false);
        $articleEntity->setOrderByClick(true);
        $articleEntity->setSource(__CLASS__);
        $articleEntity->setIsDebug(false);
        $articleEntity->setIsOnlyReturnPrimaryTag(true);
        $articleEntity->setProImageLevel(\haibao\cms\model\data\ArticleProImage::Level_FIVE);
        $this->parseTagId($dataCondition['topTagId'],$dataCondition['topTagId']);
        $this->articleAction->initArticleAction();
        $this->articleAction->setArticleEnity($articleEntity);
        list($articles,$imageColls) = $this->articleAction->getListForNew();
        $mergeData = $this->mergeData($articles['list'],$imageColls['list'],$dataCondition['limit']);
        $articleList = array();
        foreach($mergeData as $key=>$article){
            $articleArr['SiteType'] = $article->SiteType;
            $articleArr['Id'] = $article->Id;
            $articleArr['Title'] = $this->cutTitle($article->Title, 18).'...';
            $articleArr['ShortTitle'] = !empty($article->ShortTitle) ? $article->ShortTitle : $article->Title;
            $articleArr['PublishId'] = $article->PublishId;
            if($article->SiteType == ArticleController::INFO_TYPE_ARTICLE){	
            	$tag = $articles['tag'];
            	$articleTag = $articles['articleTag'];
            	$articleArr['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
            	$articleArr['TagName'] = $tag[$articleTag[$article->Id]['TagId']]['TagName'];
            	$articleArr['TagUrl'] = $tag[$articleTag[$article->Id]['TagId']]['Url'];
            	$articleArr['ProImage'] =  isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE]:
            	(isset($articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$articles['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }else{
            	$articleArr['PublishId'] = $article->Id;
            	$tag = $imageColls['tag'];
            	$collTag = $imageColls['collTag'];
            	$articleArr['Channel'] = self::getArticleUrl($article->Id,$collTag[$article->Id]['ParentId'],ArticleController::INFO_TYPE_IMAGE);
            	$articleArr['TagName'] = $tag[$collTag[$article->Id]['TagId']]['TagName'];
            	$articleArr['TagUrl'] = $tag[$collTag[$article->Id]['TagId']]['Url'];
            	$articleArr['ProImage'] =  isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE]:
            	(isset($imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])?$imageColls['proImage'][$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO]:'');
            }
            $articleArr['ProImage'] = $this->getImageLink($articleArr['ProImage'],400,400,100,0);
            array_push($articleList,$articleArr);
        }
        return $articleList;
    }
}